package com.ruoyi.web.controller.wx;


import com.paypal.api.payments.Payment;
import com.paypal.base.rest.PayPalRESTException;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.core.config.Paypal.PaypalPaymentIntent;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/wx/callback")
@Validated
public class PaypalBackResultController {

    /*@RequestMapping("/success")
    public Object success(@RequestBody String param){
        System.out.println(param);
        return AjaxResult.success("success");
    }*/

    @RequestMapping("/cancel")
    public Object cancel(@RequestBody String param){

        return AjaxResult.success("cancel");
    }


    //    /**
//     * 成功回调 + 支付 + PDT同步通知
//     *    买家确认付款，执行支付并直接返回通知
//     */
//    @RequestMapping(method = RequestMethod.GET, value = PaypalConfig.SUCCESS_URL)
    @RequestMapping("/success")
    public String successPay(@RequestParam("paymentId") String paymentId, @RequestParam("PayerID") String payerId, @RequestParam("token") String token) {
        System.out.println(paymentId);
        System.out.println(payerId);
        System.out.println(token);
        //        log.info("=========================================================================================");
//        try {
//            /**
//             * 执行支付
//             */
//            Payment payment = null;//paypalService.executePayment(paymentId, payerId);
//            if (payment.getState().equals("approved")) {
//                String id = "";     // 交易ID，transactionId
//                String state = "";  // 交易订单状态
//                String time = "";   // 交易时间
//                String custom = ""; // 本地OrderId
//                if (payment.getIntent().equals(PaypalPaymentIntent.authorize.toString())) {
//                    id = payment.getTransactions().get(0).getRelatedResources().get(0).getAuthorization().getId();
//                    state = payment.getTransactions().get(0).getRelatedResources().get(0).getAuthorization().getState();
//                    time = payment.getTransactions().get(0).getRelatedResources().get(0).getAuthorization().getCreateTime();
//                    custom = payment.getTransactions().get(0).getCustom();
//                } else if (payment.getIntent().equals(PaypalPaymentIntent.sale.toString())) {
//                    id = payment.getTransactions().get(0).getRelatedResources().get(0).getSale().getId();
//                    state = payment.getTransactions().get(0).getRelatedResources().get(0).getSale().getState();
//                    time = payment.getTransactions().get(0).getRelatedResources().get(0).getSale().getCreateTime();
//                    custom = payment.getTransactions().get(0).getCustom();
//                } else if (payment.getIntent().equals(PaypalPaymentIntent.order.toString())) {
//                    id = payment.getTransactions().get(0).getRelatedResources().get(0).getOrder().getId();
//                    state = payment.getTransactions().get(0).getRelatedResources().get(0).getOrder().getState();
//                    time = payment.getTransactions().get(0).getRelatedResources().get(0).getOrder().getCreateTime();
//                    custom = payment.getTransactions().get(0).getCustom();
//                }
////                log.info("PDT通知：交易成功回调");
////                log.info("付款人账户：" + payment.getPayer().getPayerInfo().getEmail());
////                log.info("支付订单Id {}", paymentId);
////                log.info("支付订单状态state : " + payment.getState());
////                log.info("交易订单Id：{}", id);
////                log.info("交易订单状态state : " + state);
////                log.info("交易订单支付时间：" + time);
////                log.info("本地系统OrderId：{}", custom);
////                log.info("=========================================================================================");
//                return "success";
//            }
//        } catch (PayPalRESTException e) {
//            // 如果同步通知返回异常，可根据paymentId 来查询刷新订单状态
//            // 同时IPN异步通知也可以更新订单状态
//            System.out.println(e.getMessage());
//        }
        return "redirect:/";
    }
}
